package gui;

import ge.escola.Disciplina;

import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.JButton;

import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.SoftBevelBorder;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class SelecionaDisciplina extends javax.swing.JPanel {
	private JTextArea textArea;
	private JLabel jLabel2;
	private JButton buttonAcessar;
	private JButton buttonVoltar;
	private JButton buttonExcluir;
	private JTextField caixaCodigo;
	private JLabel jLabel1;
	
	public static Disciplina disciplina;
	
	private String codigoDisciplina;
	private boolean flag;

	/**
	* Auto-generated main method to display this 
	* JPanel inside a new JFrame.
	*/
	
	public SelecionaDisciplina() {
		super();
		initGUI();
		
		String saida = "";
		
		for(Disciplina disc:GerenciadorDeJanelas.todasDisciplinas){
			saida += disc.toString();
		}
		textArea.setEditable(true);
		textArea.setText(saida);
		textArea.setEditable(false);
	}
	
	private void initGUI() {
		try {
			setPreferredSize(new Dimension(400, 300));
			this.setSize(800, 600);
			this.setBackground(new java.awt.Color(204,204,255));
			this.setLayout(null);
			{
				textArea = new JTextArea();
				this.add(textArea);
				textArea.setBounds(39, 137, 424, 407);
				textArea.setEditable(false);
				textArea.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
			}
			{
				jLabel1 = new JLabel();
				this.add(jLabel1);
				jLabel1.setText("Seleciona disciplina");
				jLabel1.setBounds(170, 17, 431, 67);
				jLabel1.setFont(new java.awt.Font("Arial",0,48));
				jLabel1.setForeground(new java.awt.Color(0,102,102));
			}
			{
				jLabel2 = new JLabel();
				this.add(jLabel2);
				jLabel2.setText("Codigo da disciplina");
				jLabel2.setBounds(554, 154, 180, 24);
				jLabel2.setFont(new java.awt.Font("Arial",0,20));
				jLabel2.setForeground(new java.awt.Color(0,102,102));
			}
			{
				caixaCodigo = new JTextField();
				this.add(caixaCodigo);
				caixaCodigo.setBounds(551, 190, 191, 29);
				caixaCodigo.setFont(new java.awt.Font("Tahoma",0,18));
				caixaCodigo.setHorizontalAlignment(SwingConstants.CENTER);
				caixaCodigo.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
			}
			{
				buttonAcessar = new JButton();
				this.add(buttonAcessar);
				buttonAcessar.setText("ACESSAR");
				buttonAcessar.setBounds(540, 231, 101, 20);
				buttonAcessar.setBackground(new java.awt.Color(255,255,255));
				buttonAcessar.setFont(new java.awt.Font("Arial",0,12));
				buttonAcessar.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED));
				buttonAcessar.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						buttonAcessarMouseClicked(evt);
					}
				});
			}
			{
				buttonVoltar = new JButton();
				this.add(buttonVoltar);
				buttonVoltar.setText("VOLTAR");
				buttonVoltar.setBounds(641, 539, 112, 21);
				buttonVoltar.setBackground(new java.awt.Color(255,255,255));
				buttonVoltar.setFont(new java.awt.Font("Arial",0,12));
				buttonVoltar.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED));
				buttonVoltar.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						buttonVoltarMouseClicked(evt);
					}
				});
			}
			{
				buttonExcluir = new JButton();
				this.add(buttonExcluir);
				buttonExcluir.setText("EXCLUIR");
				buttonExcluir.setBounds(668, 231, 101, 20);
				buttonExcluir.setBackground(new java.awt.Color(255,255,255));
				buttonExcluir.setFont(new java.awt.Font("Arial",0,12));
				buttonExcluir.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED));
				buttonExcluir.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						buttonExcluirMouseClicked(evt);
					}
				});
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void buttonAcessarMouseClicked(MouseEvent evt) {
		codigoDisciplina = caixaCodigo.getText();
		capturaDisciplina(codigoDisciplina);
		if(!flag){
			JOptionPane.showMessageDialog(null, "Disciplina nao cadastrada.");
		}else{
			GerenciadorDeJanelas.alteraDisciplina = new AlteraDisciplina();
			GerenciadorDeJanelas.window.setVisible(false);
	        GerenciadorDeJanelas.window.setContentPane(GerenciadorDeJanelas.alteraDisciplina);
	        GerenciadorDeJanelas.window.setVisible(true);
		}
	}
	
	private void buttonVoltarMouseClicked(MouseEvent evt) {
		GerenciadorDeJanelas.window.setContentPane(GerenciadorDeJanelas.menuAltera);
        GerenciadorDeJanelas.window.setVisible(true);
	}
	
	private void capturaDisciplina(String codigo){
		flag = false;
		for(Disciplina disc:GerenciadorDeJanelas.todasDisciplinas){
			if(disc.getCodigo().equals(codigo)){
				disciplina = disc;
				flag = true;
			}
		}
	}
	
	private void buttonExcluirMouseClicked(MouseEvent evt) {
		codigoDisciplina = caixaCodigo.getText();
		capturaDisciplina(codigoDisciplina);
		if(!flag){
			JOptionPane.showMessageDialog(null, "Disciplina nao cadastrada.");
		}else{
			GerenciadorDeJanelas.todasDisciplinas.remove(disciplina);
			JOptionPane.showMessageDialog(null, "Disciplina removida com sucesso!");
		}
	}

}
